package com.baymax.config;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baymax.common.BaseEntity;
import com.baymax.datascope.interceptor.MybatisPlusDataPermissionInterceptor;
import com.baymax.exception.GlobalException;
import com.baymax.utils.SecurityUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MybatisPlus配置类
 *
 * @Author: baymax
 * @CreateTime: 2023/3/28 20:47
 * @Version: 1.0
 */
@Configuration
@MapperScan(value = {"com.baymax.mapper"})
public class MybatisPlusConfig {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        // 数据权限插件
        interceptor.addInnerInterceptor(new MybatisPlusDataPermissionInterceptor());
        return interceptor;
    }

    /**
     * 自动填充功能
     */
    @Component
    static class MyMetaObjectHandler implements MetaObjectHandler {

        @Override
        public void insertFill(MetaObject metaObject) {
            try {
                if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
                    BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
                    baseEntity.setCreateTime(ObjectUtil.isNotNull(baseEntity.getCreateTime()) ? baseEntity.getCreateTime() : LocalDateTime.now());
                    baseEntity.setCreateBy(ObjectUtil.isNotNull(baseEntity.getCreateBy()) ? baseEntity.getCreateBy() : SecurityUtil.getUserId());
                    baseEntity.setUpdateTime(ObjectUtil.isNotNull(baseEntity.getUpdateTime()) ? baseEntity.getUpdateTime() : LocalDateTime.now());
                    baseEntity.setUpdateBy(ObjectUtil.isNotNull(baseEntity.getUpdateBy()) ? baseEntity.getUpdateBy() : SecurityUtil.getUserId());
                }
            } catch (Exception e) {
                throw new GlobalException("自动注入异常 => " + e.getMessage());
            }
        }

        @Override
        public void updateFill(MetaObject metaObject) {
            try {
                if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
                    BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
                    baseEntity.setUpdateTime(LocalDateTime.now());
                    baseEntity.setUpdateBy(ObjectUtil.isNotNull(baseEntity.getUpdateBy()) ? baseEntity.getUpdateBy() : SecurityUtil.getUserId());
                }
            } catch (Exception e) {
                throw new GlobalException("自动注入异常 => " + e.getMessage());
            }
        }
    }
}
